<?php

// no direct access
defined('_JEXEC') or die('Restricted access');

// Import Joomla! libraries
jimport( 'joomla.application.component.view');
jimport('joomla.html.pagination');
include_once(JPATH_COMPONENT_ADMINISTRATOR.DS.'helpers'.DS.'DataGrid.php');

class sialpacadbViewNewPet extends JView {
    function display($tpl = null) {
		global $mainframe;
		$config = new JConfig();
    	$model 	= $this->getModel();
    	
		$data 	= JRequest::get('post');
		$tbl 	= JRequest::getVar('tablename',$data['tablename']);	
		$pagemode = JRequest::getVar('pagemode',$data['pagemode']);
	  $query	= 'SHOW FIELDS FROM '.$tbl;
	  $uri 	= 'index.php?option=com_sialpacadb&view=newpet';

	  $grid = new DataGrid($tbl, $query, $activetab);
      $cctable = $grid->bindEditableGridPet($pagemode, $data);
            
      $cctable_dna = $model->getTableInfo("jos_sialpaca_dna");     
      $cctable_fibra = $model->getTableInfo("jos_sialpaca_fibra");
       //error_log(print_r($cctable_dna,true),3,"view.log");
      JToolBarHelper::title(JText::_($tbl).': <small><small>['.(($pagemode == "NEW")?JText::_('SIA_EDITROW_NEW'):JText::_('SIA_EDITROW_EDIT')).']</small></small>' );
      JToolBarHelper::save('saveRowPet');
	  //JToolBarHelper::cancel('cancelRowPet');
	  JToolBarHelper::divider();
	  JToolBarHelper::back('cancel','index.php?option=com_sialpacadb');
        
	   $this->assignRef('tables', $treeitems);
	   $this->assignRef('dbname', $config->db);
	   $this->assignRef('action', $uri);
	   $this->assignRef('tabsel', $tbl);
	   $this->assignRef('pagemode', $pagemode);
	   $this->assignRef('cctable', $cctable);
	   $this->assignRef('cctable_dna', $this->mktable($cctable_dna));
	   $this->assignRef('cctable_fibra', $this->mktable($cctable_fibra));
       parent::display($tpl);
    }
    function mktable($tabella){
		$rowIndex = 1;
		$toret = "";
		$toret .= 'var mytable = new Element("table",{name:"tblGrid", id:"tblGrid"});';
		$toret .= 'mytable.addClass("adminlist");';
		foreach ($tabella as $value)
		{
			if (strtolower($value['Field']) == 'id'){
					continue;
			}			
			$toret .= 'var mytr = Element("tr");'."\n";	
			$toret .= 'mytr.addClass("row'.($rowIndex % 2).'");';
			$toret .=  'var mytd = Element("td");'."\n";	
			$toret .=  'mytd.set("text","'.$value['Field'].'");';
			$toret .=  'mytr.grab(mytd);'."\n";
			$toret .=  'mytd = Element("td");'."\n";	
			$toret .=  'mytd.grab(new Element("input",{type:"text",name:"'.$value['Field'].'",id:"'.$value['Field'].'",value:""}).addClass("cctextfield"));'."\n";
			$toret .=  'mytr.grab(mytd);'."\n";
			$toret .=  'mytable.grab(mytr);'."\n";			
			$toret .=  'myform.grab(new Element("input",{type:"hidden",name:"colname_'.$rowIndex.'",value:"'.$value['Field'].'"}));';		
			$rowIndex++;	
		}
			$toret .=  'myform.grab(new Element("input",{type:"hidden",name:"colcount",value:"'.($rowIndex-1).'"}));';	
		
		return $toret;
	}
}
?>
